// ****************************************************************************
// HushDB
//
// @File: BufferManager.h
//
// AUTHOR:  Justin Shen
//
// NOTES: 
//     This file implements the buffer manager which allocates and manages in memory pages
//     It is also responsible for loading required page from file.
//
// ****************************************************************************

#ifndef HUSHDB_BUFFERMANAGER_H
#define HUSHDB_BUFFERMANAGER_H

#include <map>

#include "..\Common\BasicType.h"
#include "Page.h"

namespace Hush
{
    namespace Database
    { 
        class BufferManager
        {
            typedef std::map<PageId, Page*> BufferPool;
        public:
            BufferManager()
                : nextPageId(1)
            {
            }

            ~BufferManager()
            {
                for (BufferPool::iterator i = pagePool.begin(); i != pagePool.end(); i++)
                {
                    delete i->second;
                }

                pagePool.clear();
            }

            template <typename TPage>
            TPage* AllocatePage()
            {
                TPage* page = new TPage();   
                page->SetPageId(nextPageId);
                pagePool.insert(BufferPool::value_type(nextPageId, (Page*)page));

                nextPageId++;

                return page;
            }

            Page* AllocatePage()
            {
                return AllocatePage<Page>();
            }

            template <typename TPage>
            inline TPage* GetPageAs(const PageId& pageId)
            {
                return (TPage*)GetPage(pageId);
            }

            Page* GetPage(const PageId& pageId)
            {
                Page* page = 0;
                if (pagePool.find(pageId) != pagePool.end())
                {
                    page = pagePool[pageId];
                }
                return page;
            }

        private:
            PageId nextPageId;
            BufferPool pagePool;
        };
    }
}

#endif
